﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using AnimatedGif;

namespace EmotionsExtract.UITex
{
    /// <summary>
    /// UITex 动画
    /// </summary>
    public class Animation
    {
        /// <summary>
        /// 索引
        /// </summary>
        public int Index { get; private set; }

        /// <summary>
        /// 此动画包含的所有动画帧
        /// </summary>
        public AnimationFrame[] Frames { get; private set; }

        /// <summary>
        /// 获取 GIF 格式的字节流
        /// </summary>
        /// <returns>GIF 数据，可直接保存为 *.gif</returns>
        public byte[] GetImage(int binarizeThreshold = 128)
        {
            using(MemoryStream ms = new MemoryStream())
            using (AnimatedGifCreator creator = new AnimatedGifCreator(ms))
            {
                foreach (var frame in this.Frames)
                    creator.AddFrame(ImageUtils.BinarizeTransparancy(frame.GetImage(), binarizeThreshold), frame.Delay, GifQuality.Bit8);
                return ms.ToArray();
            }
        }

        public Animation(int index, AnimationFrame[] frames)
        {
            this.Index = index;
            this.Frames = frames;
        }
    }
}
